

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices.WindowsRuntime;
using UnityEngine;



public abstract partial class ActionState : FSMState
{
    private BattleController battleContoller => actionFSM.battleContoller;
    
    public  RoleEntity role => actionFSM.role;

    public PlayerActionFSM actionFSM => this.controller as PlayerActionFSM;

    /// <summary>
    /// 进入动作的时间
    /// </summary>
    protected float m_BeginTime;

    /// <summary>
    /// 在此动作的当前时间
    /// </summary>
    protected float curTime => this.battleContoller.time - m_BeginTime;

    public static int GetRealFrameCount(int frameCount,int dataCoreFPS)
   {
       return (int)(dataCoreFPS / 30.0f * frameCount);
   }

    public ActionState() : base()
    {
        
    }

    public ActionState(PlayerActionFSM controller) : base(controller)
    {
       
    }

    public override void OnEnter(FSMState preState)
    {
        base.OnEnter(preState);

        m_BeginTime = this.battleContoller.time;
    }

    public virtual void LateUpdate(float deltaTime)
    {
        
    }

}

public partial class AnyState : ActionState
{
    public override int stateId => FSMController.SignNone;

    public AnyState()
    {
    }


    public AnyState( PlayerActionFSM controller) : base(controller)
    {

    }
    
    public override void OnExit(FSMState nextState)
    {
        base.OnExit(nextState);

        GameDebug.Log($"any  准备进入 : {nextState.GetType().Name}");
    }
}


/// <summary>
/// 过程型的动作
/// </summary>
public abstract partial class ProgressAction : ActionState
{
    public float progress
    {
        get
        {
            if (m_TotalTime <= 0) return 1;

            float p =  this.curTime / (float)m_TotalTime;
            p = p > 1 ? 1 : p;
            return p;
        }
    }

    /// <summary>
    /// 动作时间
    /// </summary>
    protected float m_TotalTime = 0;
    public float totalTime => m_TotalTime;
    public bool finished => progress >= 1;

    protected float m_ExecuteTime;
    protected bool m_HasExecuted;
    public bool hasExecuted => m_HasExecuted;

    public ProgressAction() : base()
    {

    }

    public ProgressAction(PlayerActionFSM fsm) : base(fsm)
    {

    }

    public override void OnEnter(FSMState preState)
    {
        base.OnEnter(preState);
        
        m_HasExecuted = false;
    }

    public override void Update(float deltaTime)
    {
        base.Update(deltaTime);

        if (!m_HasExecuted && this.curTime >= m_ExecuteTime)
        {
            m_HasExecuted = true;
            OnExecute();
        }

        _Update(deltaTime);

    }

    protected virtual void OnExecute()
    {
        
    }

    protected virtual void _Update(float deltaTime)
    {

    }
}

/// <summary>
/// 动作状态机控制器
/// </summary>
public class PlayerActionFSM : FSMController
{
    /// <summary>
    /// 任何状态
    /// </summary>
    AnyState m_AnyState;
    public AnyState anyState => m_AnyState;

    /// <summary>
    /// 角色
    /// </summary>
    RoleEntity m_Role;
    public RoleEntity role => m_Role;
    /// <summary>
    /// 战斗数据
    /// </summary>
    public BattleController battleContoller => m_BattleController;

    private BattleController m_BattleController;

    public PlayerActionFSM(BattleController battleController,RoleEntity role)
    {
        m_Role = role;
        m_BattleController = battleController;
        
        m_AnyState = new AnyState(this);
    }

    public PlayerActionFSM()
    {
    }

    public T GetAction<T>()where T : ActionState
    {
        return this.allStates.Find(e => e.GetType().Equals(typeof(T))) as T;
    }

    protected override void _Update(float deltaTime)
    {
        base._Update(deltaTime);

        ///任何状态的跳转
        if (m_AnyState != null)
        {
            var meetTransition = m_AnyState.CheckSign(m_Sign);
            if (meetTransition != null)
            {
                var taregtState = meetTransition.toState; 
                if (m_CurState != taregtState)
                    SetCurState(taregtState);

                //m_Sign = SignNone;
            }
            else
            {
                meetTransition = m_AnyState.CheckTransition();
                if (meetTransition != null)
                {
                    FSMState taregtState = meetTransition.toState;
                    if(m_CurState != taregtState)
                        SetCurState(taregtState);
                }
            }
        }

    }

    protected override void _LateUpdate(float deltaTime)
    {
        ActionState curAction = m_CurState as ActionState;
        
        curAction?.LateUpdate(deltaTime);

        /*if (m_PreAction != null)
        {
            m_Sign = SignNone;
            m_PreAction = null;
        }*/
        m_PreAction = null;
    }

    private ActionState m_PreAction;

    protected override void OnStateChange(FSMState preState,FSMState curAction)
    {
        m_PreAction = preState as ActionState;

        if (curAction.GetType().IsSubclassOf(typeof(ProgressAction)))
        {
            m_Sign = SignNone;
        }
        
        
    }
}







